import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * A class for the configuration screen.
 * 
 * @author Srinidhi Chaganty
 *
 */
@SuppressWarnings("serial")
public class ConfigurationScreen extends JPanel{

        //array where first item is difficulty, second is type of map, third is # players
        // and fourth is whether or not to close the panel
        public Object[] configInfo;
        public Level level;

        /**
         * The constructor for the Configuration Screen that sets all the panels up
         */
        public ConfigurationScreen(){
                this.setLayout(new BorderLayout());
                configInfo = new Object[4];
                addPanels();
        }


        /**
         * Adds all the panels into the ConfigurationScreen panel
         */
        private void addPanels(){
                //the intro panel is created here
                JPanel introPanel = new JPanel();
                //the label for the configuration screen
                JLabel intro = new JLabel("CONFIGURATION SCREEN:");
                introPanel.add(intro);
                //creates the end panel
                final JPanel endPanel = endPanel();
                //adds the panels to two different panels
                JPanel addPanel1 = new JPanel();
                //the second panel to have panels added to it
                JPanel addPanel2 = new JPanel();
                addPanel1.setLayout(new BorderLayout());
                addPanel2.setLayout(new BorderLayout());
               //the panel that holds the difficulty settings
                JPanel diffPanel = difficultyPanel();
                addPanel1.add(introPanel, BorderLayout.PAGE_START);
                addPanel1.add(diffPanel, BorderLayout.CENTER);
                //the panel that holds the map settings
                JPanel mapPanel = mapPanel();
                addPanel2.add(mapPanel, BorderLayout.PAGE_START);
                //the panel that holds the number of players settings
                JPanel numPlayerPanel = numberPlayersPanel();
                addPanel2.add(numPlayerPanel,BorderLayout.CENTER);
                //creating the game.dat file
                File f = new File("game.dat");
                if(f.exists()) {
            		//the panel for the load button
                    JPanel loadPanel = loadPanel();
                    addPanel2.add(loadPanel, BorderLayout.SOUTH);
                }
                //adds everything to the ConfigurationScreen panel
                this.add(addPanel1, BorderLayout.PAGE_START);
                this.add(addPanel2, BorderLayout.CENTER);
                this.add(endPanel, BorderLayout.PAGE_END);
        }

        /**
         * sets up a panel for choosing number of players
         * 
         * @return JPanel the panel for choosing the number of players
         */
        private JPanel numberPlayersPanel() {
    		//creates the panel to be returned
            JPanel numPanel = new JPanel();
            //the label to ask how many players
            JLabel playerLabel = new JLabel("How many players? ");
            //the button for 2 players- sets the configInfo[2] to 2 players when pressed
            final JButton twoButton = new JButton ("2");
            twoButton.setBackground(Color.YELLOW);
            twoButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if (e.getSource() == twoButton) {
                                    configInfo[2] = 2;
                                    GameModel.getInstance().setNumPlayers(2);
                            }}});
          //the button for 3 players- sets the configInfo[2] to 3 players when pressed
            final JButton threeButton = new JButton ("3");
            threeButton.setBackground(Color.CYAN);
            threeButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if (e.getSource() == threeButton) {
                                    configInfo[2] = 3;
                                    GameModel.getInstance().setNumPlayers(3);
                            }}});
          //the button for 4 players- sets the configInfo[2] to 4 players when pressed
            final JButton fourButton = new JButton ("4");
            fourButton.setBackground(Color.ORANGE);
            fourButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if (e.getSource() == fourButton) {
                                    configInfo[2] = 4;
                                    GameModel.getInstance().setNumPlayers(2);
                            }}});
            numPanel.add(playerLabel);
            numPanel.add(twoButton);
            numPanel.add(threeButton);
            numPanel.add(fourButton);
            return numPanel;
        }

        /**
         * sets up a panel for choosing the type of map
         * 
         * @return JPanel the panel for choosing the type of map
         */
        private JPanel mapPanel() {
        	//creates the panel to be returned
            JPanel mapChoice = new JPanel();
            //the label asking user to select their map type
            JLabel mapLabel = new JLabel("Select your map type: ");
            //the button for a standard map- when pressed, sets configInfo[1] to standard
            final JButton standardButton = new JButton("STANDARD");
            standardButton.setBackground(Color.MAGENTA);
            standardButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if(e.getSource() == standardButton) {
                                    configInfo[1] = "Standard";
                                    GameModel.getInstance().setTypeMap("standard");
                                    }
                            }});
          //the button for a random map- when pressed, sets configInfo[1] to random
            final JButton randomButton = new JButton ("RANDOM");
            randomButton.setBackground(Color.BLUE);
            randomButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if (e.getSource() == randomButton) {
                                    configInfo[1] = "Random";
                                    GameModel.getInstance().setTypeMap("Random");
                            }
                            }});
            mapChoice.add(mapLabel);
            mapChoice.add(standardButton);
            mapChoice.add(randomButton);
            return mapChoice;
        }

        /**
         * sets up a panel for the next button
         * 
         * @return JPanel the panel for the next button
         */
        private JPanel endPanel(){
        	//creates the panel to be returned
            final JPanel endPanel = new JPanel();
            //the next button that checks if all fields have been filled in
            final JButton nextButton = new JButton("Next: ");
            //next button checks if all the fields have been filled in
            nextButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if (e.getSource() == nextButton)
                                    if(configInfo[0] != null && configInfo[1] != null && configInfo[2] != null)
                                            configInfo[3] = true;
                                    else{
                                            configInfo[3] = false;
                                            //error label saying that the user needs to fill in more fields
                                            JLabel error = new JLabel("Please fill in all the fields");
                                            endPanel.add(error);
                                    }}});
            endPanel.add(nextButton);
            return endPanel;
        }

        /**
         * sets up a panel for choosing the difficulty
         * 
         * @return JPanel the panel for choosing the difficulty
         */
        private JPanel difficultyPanel(){
        	//creates the panel to be returned
            JPanel diff = new JPanel();
            //label that asks user to select their difficulty level
            JLabel difficulty = new JLabel("Select your difficulty level: ");
            //button for the easy level that sets difficulty to easy
            final JButton easyButton = new JButton("EASY LEVEL");
            easyButton.setBackground(Color.GREEN);
            easyButton.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if(e.getSource() == easyButton){
                                    configInfo[0] = "Easy";
                                    GameModel.getInstance().setDifficulty("Easy");
                                    }}});
          //button for the hard level that sets difficulty to hard
            final JButton hardButton = new JButton("HARD LEVEL");
            hardButton.setBackground(Color.RED);
            hardButton.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if(e.getSource() == hardButton){
                                    configInfo[0] = "Hard";
                                    GameModel.getInstance().setDifficulty("Hard");
                            }}});
            diff.add(difficulty);
            diff.add(easyButton);
            diff.add(hardButton);
            return diff;
        }
        
        /**
         * Load Panel
         * 
         * @return the JPanel with the load button
         */
        
        private JPanel loadPanel() {
        	//creates the panel to be returned
            JPanel load = new JPanel();
            //the button for loading a game
            final JButton loadButton = new JButton("LOAD GAME");
            loadButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
            		//creates a game model
                    GameModel game = GameModel.getFromFile("game.dat");
                    GameModel.getInstance().setDifficulty(game.getDifficulty());
                    GameModel.getInstance().setTypeMap(game.getTypeMap());
                    GameModel.getInstance().setNumPlayers(game.getNumPlayers());
                    GameModel.getInstance().setPlayerArr(game.getPlayerArr());
                    GameModel.getInstance().setPlayers(game.getPlayers());
                    GameModel.getInstance().setTurnTimer(game.getTurnTimer());
                    GameModel.getInstance().setNumRounds(game.getNumRounds());
                    GameModel.getInstance().setRound(game.getRound());
                    GameModel.getInstance().setCurrPlayer(game.getCurrPlayer());
                    GameModel.getInstance().setMap(game.getMap());
                    GameModel.getInstance().getRound().setRoundNum(game.getRound().getRoundNum());
                    GameModel.getInstance().setRoundsLeft(game.getRoundsLeft());
                    //creates a GameController
                    GameController gameCon = new GameController();
                    gameCon.setVisible(true);
                    //creates a GameView
                    GameView gView = GameView.getInstance();
                    gView.getMain().setVisible(true);
                    for (int i = GameModel.getInstance().getRound().getRoundNum(); i < GameModel.getInstance().getNumRounds(); i++) {
                        System.out.println("Round Number: " + GameModel.getInstance().getRound().getRoundNum());
                        //sorts players
                        GameModel.getInstance().sortPlayers(GameModel.getInstance().getPlayers());
                        //create a new round
                        GameModel.getInstance().setRound(new Round());
                        GameModel.getInstance().getRound().setRoundNum(i);
                        if(i == 0) JOptionPane.showMessageDialog(gameCon,"GAME IS STARTING");
                        else JOptionPane.showMessageDialog(gameCon,"NEW ROUND IS STARTING");
                        JOptionPane.showMessageDialog(gameCon,"Player " + GameModel.getInstance().getPlayers().get(0).getName() +
                                        " goes first.");
                        gameCon.validate();
                        gameCon.repaint();
                        
                        //TURN FOR-LOOP
                        for(int k = 0; k < GameModel.getInstance().getNumPlayers(); k++){
                            Player p = GameModel.getInstance().getPlayers().get(k);
                            GameModel.getInstance().setCurrPlayer(p);
                            gView.updateMain();
                            MainScreen ms = gView.getMain();
                            gameCon.add(ms);
                            gameCon.pack();
                            JOptionPane.showMessageDialog(gameCon,"Now starting Player " + p.getName() +
                                            "'s Turn.");
                            //creates a new turn based on the player and their turn time
                            GameModel.getInstance().setTurn(new Turn(p, p.getTurnTime()));
                            GameModel.getInstance().getTurn().getT().runTimer();
                            while (!GameModel.getInstance().getTurn().getT().turnEnded()){
                            	ms.setVisible(true);
                            }
                            gView.updateMain();
                            gameCon.remove(ms);
                            gameCon.validate();
                            gameCon.repaint();
                            //gameCon.pack();
                        }
                    }
                }
            });
            load.add(loadButton);
            return load;
        }
}